// pages/Census/customer-list.js
const Controllers = require('../../controllers/ControllerCensus').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    tabs: [
      {
        value: 10000.00,
        text: '店铺租金收益',
        key: 'rent',
        type: 8
      },
      {
        value: 10000.00,
        text: '云商品收益',
        key: 'cloud',
        type: 7
      }
    ],
    active: 0,
    page: {
      pageNumber: 1,
      pageSize: 10
    },
    list: [],
    totalRow: 0,
    isLoading: false,
    lastPage: true
  },
  changeTab(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      active: index,
      page: {
        pageNumber: 1,
        pageSize: 10
      }
    })
    that.shopDataDetailPage();
  },
  shopDataDetail() {
    var that = this;
    const shopId = that.data.shopId;
    Controllers.shopDataDetail({shopId}).then(res => {
      console.log('shopDataDetail--res==', res);
      const { code, data, desc } = res;
      that.setData({
        shopData: data
      })
    })
  },
  shopDataDetailPage() {
    var that = this;
    var page = that.data.page;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    console.log('that.data.tabs[that.data.active==', that.data.tabs[that.data.active]);
    
    const type = that.data.tabs[that.data.active].type;
    wx.showLoading();
    var params = { ...page, shopId: that.data.shopId, type };
    Controllers.shopDataDetailPage(params).then(res => {
      console.log('shopDataDetailPage--res==', res);
      const { code, data, desc } = res;
      const { list, totalRow, lastPage } = data;
      if (page.pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      that.setData({
        list: that.data.list,
        totalRow,
        lastPage,
        isLoading: false
      });
      wx.hideLoading();
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      shopId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.shopDataDetail();
    that.shopDataDetailPage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  onReachBottom: function () {
    var that = this;
    if (that.data.isLoading || that.data.lastPage) {
      return;
    }
    that.data.page.pageNumber += 1;
    that.shopDataDetailPage();
  },
    

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})